<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Ot_FrontController_Plugin_Language
 * @category   Front Controller Plugin
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */

/**
 * Language front controller plugin to provide i18n to our apps.
 *
 * @package    Ot_FrontController_Plugin_Language
 * @category   Front Controller Plugin
 */
class Ot_FrontController_Plugin_Language extends Zend_Controller_Plugin_Abstract
{

	public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
		$locale = new Zend_Locale();
		
		$options = array(
			'scan' => Zend_Translate::LOCALE_FILENAME,
			'clear' => false,
		    'disableNotices' => true
		);
		
		$config = Zend_Registry::get('config');
							
		Zend_Translate::setCache(Zend_Registry::get('cache'));
		
		if (isset($_COOKIE['language_select'])) {
			$language = $_COOKIE['language_select'];
		} else {
			$language = $config->user->language->val;
		}
		
		$translate = new Zend_Translate('csv', APPLICATION_PATH . '/../overrides/languages', 'auto', $options);
		
		$translate->addTranslation(APPLICATION_PATH . '/languages');

		
		if (!$translate->isAvailable($language)) {
			throw new Exception('Language ' . $language . ' is not available');
		}
								
		$locale->setLocale($language);
		$translate->setLocale($locale);
		
		Zend_Registry::set('Zend_Locale', $locale);
		Zend_Registry::set('Zend_Translate', $translate);
	}
}